/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.cicp.collector.oracle;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.AbstractJDBCCollector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * <p>
 * Description: Oracle吞吐量查询。程序返回每秒用户提交数，每秒用户回滚数 ，每秒调用次数
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-25 下午05:41:12          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class ThroughputCollector extends AbstractJDBCCollector {

    private static final Logger logger = Logger.getLogger(ThroughputCollector.class);

    private double[] lastValue = null;

    private long lastTime = -1;

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String dbname = this.getParamMap().get("DBName").trim();
        String dbtype = this.getParamMap().get("DBType").trim();
        String sql = this.getParamMap().get("SQL").trim();
        String[] propMetas = getPropMeta();
        double[] tmpValues = new double[propMetas.length];
        try {
            ResultSet rs = this.getCachedResultSet(dbtype, ip, dbname, sql);
            int k = 0;
            while (rs.next()) {
                tmpValues[k++] = rs.getDouble(2);
            }
            if (lastTime > -1 && lastValue != null) {
                ResPropertyValue propertyValueX = new ResPropertyValue();
                ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[tmpValues.length];
                for (int i = 0; i < propValues.length; i++) {
                    double v = ((tmpValues[i] - lastValue[i]) / (System.currentTimeMillis() - lastTime)) * 1000;
                    v = ((long) (v * 10d)) / 10d;
                    propValues[i] = ResPropertyValue.createPropValue(String.valueOf(v), propMetas[i]);
                }
                propertyValueX.setValues(propValues);
                values.add(propertyValueX);
            }
            lastTime = System.currentTimeMillis();
            lastValue = tmpValues;
        } catch (Exception e) {
            logger.error("oracle数据库吞吐量采集失败", e);
        }
        return values;
    }

    @Override
    public String[] getParamMeta() {
        return new String[] { "DBName", "DBType", "SQL" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "user commits", "user rollbacks", "user calls" };
        return new String[] { "$1", "$2", "$3" };
    }

}
